package com.example.searchcenter.center;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.example.searchcenter.R;
import com.example.searchcenter.helper.ConvertUnsigned;
import com.example.searchcenter.helper.ImageLoader;
import com.example.searchcenter.model.CenterItemModel;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.ImageView;
import android.widget.TextView;

@SuppressLint("InflateParams")
public class CenterAdapter extends BaseAdapter{
    
    private Activity activity;
    private ArrayList<CenterItemModel> centerItemList;
    private List<CenterItemModel> centerlist = null;
    LayoutInflater inflater;
    public ImageLoader imageLoader; 
    
    public CenterAdapter(Activity a, List<CenterItemModel> list) {
    	activity = a;
        this.centerlist=list;
        this.centerItemList = new ArrayList<CenterItemModel>();
        this.centerItemList.addAll(list);
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public class ViewHolder {
		TextView title;
		TextView address;
		TextView createDate;
		ImageView thumbnai;
	}

	@Override
	public int getCount() {
		return centerlist.size();
	}

	@Override
	public CenterItemModel getItem(int position) {
		return centerlist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View view, ViewGroup parent) {
		final ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = inflater.inflate(R.layout.list_item_center, null);
			// Locate the TextViews in listview_item.xml
			holder.title = (TextView) view.findViewById(R.id.title);
			holder.address = (TextView) view.findViewById(R.id.address);
			holder.createDate = (TextView) view.findViewById(R.id.createDate);
			holder.thumbnai = (ImageView) view.findViewById(R.id.list_image);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		// Set the results into TextViews
		
		holder.title.setText(centerlist.get(position).getCenterName());
		holder.address.setText(centerlist.get(position).getCenterAddress());
		holder.createDate.setText(centerlist.get(position).getCreateDate());
		imageLoader.DisplayImage(centerlist.get(position).getThumbnaiImage(), holder.thumbnai);
		
		return view;
	}

	// Filter Class
	public void filter(String charText) {
		charText = charText.toLowerCase(Locale.getDefault());
		centerlist.clear();
		if (charText.length() == 0) {
			centerlist.addAll(centerItemList);
		} 
		else 
		{
			for (CenterItemModel wp : centerItemList) 
			{
				ConvertUnsigned convert = new ConvertUnsigned();
				String convertName = convert.ConvertString(wp.getCenterName());
				String convertAddress = convert.ConvertString(wp.getCenterAddress());
				if (convertName.toLowerCase(Locale.getDefault()).contains(charText) || convertAddress.toLowerCase(Locale.getDefault()).contains(charText) ) 
				{
					centerlist.add(wp);
				}
			}
		}
		notifyDataSetChanged();
	}
    
}